package com.ohos.carbon.slice.componentList;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.ComponentDataBean;
import com.ohos.carbon.provider.ComponentAvatarProvider;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import ohos.agp.components.element.ElementScatter;

import java.util.ArrayList;
import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class AvatarAndTextSlice extends AbilitySlice {

    private Image back_button;
    private Text text_title, text;
    private ListContainer listContainer;
    private ArrayList<ComponentDataBean> mList;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_avatar_and_text);
        initData();
        initView();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Avatar and text...")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Avatar and text...")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Avatar and text...", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Avatar and text...", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Avatar and text...", 1);
            pf.flushSync();
        }
    }

    private void initData() {
        mList = new ArrayList<>();
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man0, "Phyllis Carter", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man1, "Fgudbk Vjoidn Bhjs", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man2, "Nhudsb Shidb Ghsib", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man3, "Bhdgd Chiusbubdk", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man4, "Dhsniwgid Bhibd Hhsihd", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man5, "Gusbvd Zhusbi Gisbq", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man6, "Ahibdi Zinbd Hkndolq", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man7, "Qjond Sjowd Hoqndo", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man8, "Jhidn Qodnd Zjond", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man9, "Ljodn Sidbobd", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man0, "Whibdod Ankndobfc Gisb", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man1, "Ihindod Snbiobd", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man2, "Mjond Thidbobc", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man3, "Libdn Qhibdn", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man4, "Gjihd Mhib Mibd", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man5, "Dhibd Xhibd Hnibd", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man6, "Qnhibd Lnond Pjond", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man7, "Ybiubd Vhoihd Onhibd", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man8, "Ubjbdsu Knond", null, null));
        mList.add(new ComponentDataBean(ResourceTable.Media_randomdata_man9, "Ehibd Tbuvd Whibdn", null, null));


    }

    private void initView() {
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        text_title.setText("Avatar and text...");
//        text = (Text) findComponentById(ResourceTable.Id_text);
//        text.setText(ResourceTable.String_text);
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_listview);
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        listContainer.setItemProvider(new ComponentAvatarProvider(mList, getContext()));
        back_button.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
